package com.dchh.android.suverynew.views;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.view.MyPopupWindow;

/**
 * 操作弹出窗
 *
 * @author zhoudan
 * @date 2015年5月27日
 */
public class OperatePopupWindow {

    private MyPopupWindow window;
    private LinearLayout btn1;
    private LinearLayout btn2;
    private Context context;

    private OnClickListener listener1;
    private OnClickListener listener2;

    /***
     * 构造方法
     *
     * @param activity
     * @param listener1 导航按钮点击监听
     * @param listener2 编辑按钮点击监听
     */
    public OperatePopupWindow(Context context, OnClickListener listener1, OnClickListener listener2) {
        this.context = context;
        this.listener1 = listener1;
        this.listener2 = listener2;
    }

    /**
     * @param parent
     * @param type   0--全部显示，1--只显示‘采集’
     */
    public void showWindow(View parent, int type) {
        if (window == null) {
            View v = LayoutInflater.from(context).inflate(R.layout.util_bottom_operate_poup, null);

            btn1 = (LinearLayout) v.findViewById(R.id.btn1);
            btn2 = (LinearLayout) v.findViewById(R.id.btn2);

            btn1.setOnClickListener(listener1);
            btn2.setOnClickListener(listener2);

            window = new MyPopupWindow(v, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
        }

        if (type == 1) {
            btn1.setVisibility(View.GONE);
        } else {
            btn1.setVisibility(View.VISIBLE);
        }

        window.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    public void dismiss() {
        if (window != null) {
            window.dismiss();
        }
    }


    //===================================================================

    public OperatePopupWindow(Context context) {
        this.context = context;
    }

    public void show(View parent, String text, OnClickListener listener1) {
        if (window == null) {
            View v = LayoutInflater.from(context).inflate(R.layout.util_bottom_operate_poup, null);

            btn1 = (LinearLayout) v.findViewById(R.id.btn1);
            btn2 = (LinearLayout) v.findViewById(R.id.btn2);

            TextView tv = (TextView) v.findViewById(R.id.btn_text);
            tv.setText(text);

            btn2.setVisibility(View.GONE);

            btn1.setOnClickListener(listener1);

            window = new MyPopupWindow(v, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true);
        }

        window.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

}
